package org.shj.thread.book2.chapter10;

public class CountupThread extends Thread{

	private volatile long counter = 0;
	
	private volatile boolean shutdownRequested = false;
	
	public void shutdownRequest(){
		this.shutdownRequested = true;
		//interrupt(); //中断线程的sleep（） 或 wait()
	}
	
	public boolean isShutdownRequested(){
		return shutdownRequested;
	}
	
	public void run(){
		try{
			while (!shutdownRequested){
				doWork();
			}
		}catch(InterruptedException e){
			
		}finally{
			doShutdown();
		}
	}
	
	private void doWork() throws InterruptedException{
		counter++;
		System.out.println("doWork: counter = " + counter);
		Thread.sleep(500);
	}
	
	private void doShutdown(){
		System.out.println("doShutdown: counter = " + counter);
	}
}
